import { defineStore } from 'pinia'
import { SelfStorage } from '@/utils/secure-ls.ts'
import system from '@/api/modules/system.ts'
import { useSysKeepAliveStore } from '@/stores/use-keep-alive-store.ts'
import { useSysViewTagsStore } from '@/stores/use-view-tags-store.ts'

export const useMenuStore = defineStore('menu', {
    state: () => {
        return {
            permissions: [] as string[], // 权限
            menu: [] as Menu.MenuOptions[], // 菜单列表
            breadcrumbList: [] as Menu.MenuOptions[], // 面包屑导航
            dashboardGrid: [] as string[], // 首页布局
            isRoutesLoaded: false
        }
    },
    actions: {
        // 刷新菜单
        async refreshCache() {
            const sysViewTagsStore = useSysViewTagsStore()
            const sysKeepAliveStore = useSysKeepAliveStore()
            sysViewTagsStore.$reset()
            sysKeepAliveStore.$reset()

            const res = await system.tool.refreshCache.get()
            this.menu = res.menu
            this.permissions = res.permissions
            this.dashboardGrid = res.dashboardGrid
        }
    },
    persist: {
        key: 'MENU',
        storage: SelfStorage
    }
})
